// packageI/pages/laboratoryDetail/laboratoryDetail.js
const app = getApp()
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: applet_park_api_url_imgUrl,
    openid: wx.getStorageSync("OPENID"),
    token: wx.getStorageSync('TOKEN'),
    laboratory: '',
    banners: [],
    laboratoryId: '1',
    today: util.formatTime(new Date).substring(0, 7),
    inDate: util.formatTime(new Date).substring(0, 7),
    choseIds: [],
    price: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.data.laboratoryId = options.id
    that.setData(that.data)
    that.getInfos()
  },

  submit() {
    var that = this;
    if (that.data.choseIds == '') {
      wx.showToast({
        title: '请选择时段',
        icon: 'none'
      })
      return
    }
    var start = that.data.choseIds[0]
    var end = that.data.choseIds[that.data.choseIds.length - 1]
    wx.request({
      url: applet_park_api_url + '/api/wechatPay/laboratory',
      method: 'POST',
      header: {
        Authorization: that.data.token,
      },
      data: {
        openid: that.data.openid,
        laboratoryId: that.data.laboratoryId,
        orderStartDate: that.data.inDate.substring(0, 4) + '-' + that.data.laboratory.calenderFlags[start].currentDate,
        orderEndDate: that.data.inDate.substring(0, 4) + '-' + that.data.laboratory.calenderFlags[end].currentDate,
        totalAmount: that.data.price + '00'
      },
      success: function (res) {
        if (res.data.code == 0) {
          wx.requestPayment({
            timeStamp: res.data.data.timeStamp,
            nonceStr: res.data.data.nonceStr,
            package: res.data.data.prepay_id,
            signType: 'MD5',
            paySign: res.data.data.sign,
            paySource: 1,
            success(res) {
              wx.showToast({
                title: '支付成功',
              })
              setTimeout(function () { //刷新列表
                wx.navigateBack({
                  delta: 1, //返回的页面数，如果 delta 大于现有页面数，则返回到首页。
                  success: function () {
                    var pages = getCurrentPages()
                    var beforePage = pages[pages.length - 1]; //获取上一个页面实例对象
                    beforePage.goOrders(); //触发父页面中的方法
                  },
                })
              }, 500);
            },
            fail() {
              wx.navigateBack({
                delta: 1, //返回的页面数，如果 delta 大于现有页面数，则返回到首页。
                success: function () {
                  var pages = getCurrentPages()
                  var beforePage = pages[pages.length - 1]; //获取上一个页面实例对象
                  beforePage.goOrders(); //触发父页面中的方法
                },
              })
            }
          })

        } else {
          wx.showToast({
            title: '支付失败',
          })
        }
      }
    })
  },

  changeMonth(e) {
    var that = this;
    var time = new Date(that.data.inDate)
    if (e.currentTarget.dataset.chose == 1) {
      time = time.setMonth(time.getMonth() - 1)
    } else if (e.currentTarget.dataset.chose == 2) {
      time = time.setMonth(time.getMonth() + 1)
    } else {
      return
    }
    that.data.inDate = util.formatTime(time).substring(0, 7)
    that.setData(that.data)
    that.getInfos()
    that.data.choseIds = []
    that.data.price = 0
  },

  chose(e) {
    var index = e.currentTarget.dataset.index
    var that = this
    if (that.data.laboratory.calenderFlags[index].orderedFlag == true) {
      wx.showToast({
        title: '该时段不可选',
      })
      return
    }

    if (that.data.choseIds.length == 0) {
      that.data.laboratory.calenderFlags[index].orderedFlag = 3
      that.data.choseIds.push(index)
    } else {
      if (index == that.data.choseIds[0]) {
        that.data.laboratory.calenderFlags[index].orderedFlag = false
        that.data.choseIds.splice(0, 1)
      } else if (index == that.data.choseIds[that.data.choseIds.length - 1]) {
        that.data.laboratory.calenderFlags[index].orderedFlag = false
        that.data.choseIds.splice(that.data.choseIds.length - 1, 1)
      } else if (index + 1 == that.data.choseIds[0] || index - 1 == that.data.choseIds[that.data.choseIds.length - 1]) {
        that.data.laboratory.calenderFlags[index].orderedFlag = 3
        that.data.choseIds.push(index)
      } else {
        wx.showToast({
          title: '日期需连续',
        })
        return
      }
      that.data.choseIds.sort();
    }
    that.data.price = that.data.choseIds.length * that.data.laboratory.price
    that.setData(that.data)
  },

  getInfos() {
    var that = this;
    wx.request({
      url: applet_park_api_url + '/api/laboratory/orderFlagList',
      method: 'GET',
      data: {
        laboratoryId: that.data.laboratoryId,
        inDate: that.data.inDate + '-01'
      },
      success: function (res) {
        if (res.data.code == 0) {
          that.data.banners = res.data.data.images.split(',')
          res.data.data.openStartTime = res.data.data.openStartTime.substring(0, 5)
          res.data.data.openEndTime = res.data.data.openEndTime.substring(0, 5)
          for (var i in res.data.data.calenderFlags) {
            res.data.data.calenderFlags[i].currentDate = res.data.data.calenderFlags[i].currentDate.substring(5)
          }
          that.data.laboratory = res.data.data;
          that.setData(that.data);
        } else {
          wx.showToast({
            title: res.data.data
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})